package cn.zxf.note.user.addition;

import cn.zxf.note.user.addition.dto.UserAdditionDto;
import com.github.zengxf.common.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by zengxf on 2019/7/29.
 */
@Component
public class UserAdditionService {

    @Autowired
    private UserAdditionRepository repos;

    public UserAdditionDto findOne(Integer userId) {
        UserAddition ua = repos.findByUserId(userId);
        return BeanUtils.transform(ua, UserAdditionDto::new);
    }

    public void save(Integer userId, String nick) {
        UserAddition ua = repos.findByUserId(userId);
        if (ua == null) {
            ua = new UserAddition();
            ua.setUserId(userId);
            ua.setLevel(UserLevelEnum.LEVEL_1.code);
        }
        ua.setNick(nick);
        repos.save(ua);
    }

}
